package com.springDemo.JDBC.service;

import org.junit.BeforeClass;
import org.junit.Test;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowCallbackHandler;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.jdbc.datasource.DriverManagerDataSource;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2016/7/17.
 */
public class JdbcTempletCallBack {

    private static JdbcTemplate jt;
    private static NamedParameterJdbcTemplate npjd;

    @BeforeClass
    public static void setUpClass() {
        String url = "jdbc:mysql://127.0.0.1:3306/mysql";
        DriverManagerDataSource dataSource = new DriverManagerDataSource(url, "root", "");
        dataSource.setDriverClassName("org.gjt.mm.mysql.Driver");
        jt = new JdbcTemplate(dataSource);
        npjd = new NamedParameterJdbcTemplate(dataSource);
    }

    @Test
    public void query() {
        String sql = "select * from sys_user WHERE username=:NAME";
        Map paramMap = new HashMap();
        paramMap.put("NAME","zhoudada");
        List<Map<String,Object> > valueMap = npjd.queryForList(sql,paramMap);
        for(Map<String,Object> values:valueMap) {
            System.out.println(values.get("username"));
        }
    }

}
